#ifndef CPP_ACTIVITY_FACTORY_H
#define CPP_ACTIVITY_FACTORY_H

#include <map>
#include <string>

typedef void* (*Constructor)();

//@brief 活动工厂
//@author cvte
class ActivityFactory {
   public:
    static void registerClass(std::string className, Constructor constructor) {
        constructors()[className] = constructor;
    }

    static void* createActivity(const std::string& className) {
        Constructor constructor = NULL;

        if (constructors().find(className) != constructors().end())
            constructor = constructors().find(className)->second;

        if (constructor == NULL)
            return NULL;

        return (*constructor)();
    }

   private:
    inline static std::map<std::string, Constructor>& constructors() {
        static std::map<std::string, Constructor> instance;
        return instance;
    }
};

//注册活动类模板
#define REGISTER_ACTIVITY_CLASS(class_name)                                                      \
    class class_name##Helper {                                                                   \
       public:                                                                                   \
        class_name##Helper() {                                                                   \
            ActivityFactory::registerClass(#class_name, class_name##Helper::createActivityFunc); \
        }                                                                                        \
        static void* createActivityFunc() {                                                      \
            return new class_name;                                                               \
        }                                                                                        \
    };                                                                                           \
    class_name##Helper class_name##helper;

#endif /* CPP_ACTIVITY_FACTORY_H */